package cn.iocoder.yudao.module.trade.controller.admin.fruitcard;

import cn.iocoder.yudao.module.trade.controller.admin.fruitcard.vo.*;
import cn.iocoder.yudao.module.trade.convert.fruitcard.FruitCardDetailConvert;
import cn.iocoder.yudao.module.trade.dal.mysql.fruitcard.FruitCardDetailDO;
import cn.iocoder.yudao.module.trade.service.fruitcard.FruitCardDetailService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 水果卡明细")
@RestController
@RequestMapping("/fruit/card-detail")
@Validated
public class FruitCardDetailController {

    @Resource
    private FruitCardDetailService cardDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建水果卡明细")
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:create')")
    public CommonResult<Long> createCardDetail(@Valid @RequestBody FruitCardDetailCreateReqVO createReqVO) {
        return success(cardDetailService.createCardDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水果卡明细")
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:update')")
    public CommonResult<Boolean> updateCardDetail(@Valid @RequestBody FruitCardDetailUpdateReqVO updateReqVO) {
        cardDetailService.updateCardDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水果卡明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:delete')")
    public CommonResult<Boolean> deleteCardDetail(@RequestParam("id") Long id) {
        cardDetailService.deleteCardDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水果卡明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:query')")
    public CommonResult<FruitCardDetailRespVO> getCardDetail(@RequestParam("id") Long id) {
        FruitCardDetailDO cardDetail = cardDetailService.getCardDetail(id);
        return success(FruitCardDetailConvert.INSTANCE.convert(cardDetail));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水果卡列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:query')")
    public CommonResult<List<FruitCardDetailRespVO>> getCardDetailList(@RequestParam("ids") Collection<Long> ids) {
        List<FruitCardDetailDO> list = cardDetailService.getCardDetailList(ids);
        return success(FruitCardDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水果卡明细分页")
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:query')")
    public CommonResult<PageResult<FruitCardDetailRespVO>> getCardDetailPage(@Valid FruitCardDetailPageReqVO pageVO) {
        PageResult<FruitCardDetailDO> pageResult = cardDetailService.getCardDetailPage(pageVO);
        return success(FruitCardDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水果卡明细 Excel")
    @PreAuthorize("@ss.hasPermission('fruit:card-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportCardDetailExcel(@Valid FruitCardDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FruitCardDetailDO> list = cardDetailService.getCardDetailList(exportReqVO);
        // 导出 Excel
        List<FruitCardDetailExcelVO> datas = FruitCardDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水果卡明细.xls", "数据", FruitCardDetailExcelVO.class, datas);
    }

}
